<?php

ini_set("display_errors", 0);

error_reporting(E_ALL ^ E_NOTICE);

error_reporting(E_ALL ^ E_WARNING);



header("Content-type: text/html; charset=utf-8");



$time = time();

$servername = 'localhost';
$username = 'root';  //用户名
$password = 'root';  //密码
$dbname = "ejfx";   //数据库
$conn=mysql_connect($servername, $username, $password) or die('连接失败');
mysql_select_db($dbname);
mysql_query('set names utf8');


//捐赠开关状态
$seaton = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'seat_on' "));

$seaton = $seaton['value'];





//占位金额

$seatmoney = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'seat_money' "));

$seatmoney = $seatmoney['value'];


//当前轮次开始时间
$now_list_time = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'now_list_time'"));

$now_list_time = $now_list_time['value'];



//周期

$jz_countdown = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'jz_countdown'"));

$jz_countdown = $jz_countdown['value'];
$jz_countdown = 60*60*$jz_countdown; //转换成时间戳


$end_jz_time = $now_list_time+$jz_countdown-1; //结算时间。


if($time >= $end_jz_time){ //到达结算时间

    //当前轮次
    $now_list_num = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'now_list_num'"));

    $now_list_num = $now_list_num['value'];

    //最后占位者
    $lastseat = mysql_fetch_array(mysql_query("SELECT user_id FROM erj_seatlist WHERE  list_num = '$now_list_num' ORDER BY time DESC  LIMIT 0,1"));

    $lastseat = $lastseat['user_id'];

    //奖池金额
    $seat = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'pond' "));

    $seat = $seat['value'];



    if($lastseat >0){
        //最后占位者获得奖金
        $now_usermoney = mysql_fetch_array(mysql_query("SELECT usermoney FROM erj_user WHERE  id = '$lastseat'"));

        $now_usermoney = $now_usermoney['usermoney'] + $seat;

        mysql_query("UPDATE erj_user SET usermoney = $now_usermoney WHERE id = '$lastseat'");

        //mysql_query("UPDATE erj_user SET usermoney = usermoney + $seat WHERE id = '$lastseat'");

    }


    //插入记录
    $user = $lastseat;

    $money = $seat;

    $type = 8;

    $msg =  '第'.$now_list_num.'轮，奖池结算奖励，获得'.$seat;



    if($lastseat >0){

        $ins = mysql_query("INSERT INTO erj_log (user_id,money,amount,type ,  msg, date) VALUES ($user,$money,0,$type,'$msg',$time)");

    }


    //清空奖池




    $pond['value'] = 0;

    $now_jz_time['value'] = time();



    mysql_query("UPDATE erj_sys SET value = 0 WHERE name = 'pond'");

    //重置开始时间
    if($lastseat >0){

        mysql_query("UPDATE erj_sys SET value = $time WHERE name = 'now_list_time'");

        mysql_query("UPDATE erj_sys SET value = value + 1 WHERE name = 'now_list_num'");

    }else{

        mysql_query("UPDATE erj_sys SET value = $time WHERE name = 'now_list_time'");

    }



    //执行系统占位前4层
    $list_num = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'now_list_num' "));

    $list_num = $list_num['value'];



    $in_pond = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'in_pond' "));

    $in_pond = $in_pond['value']*15;

    $in_list = mysql_fetch_array(mysql_query("SELECT value FROM erj_sys WHERE  name = 'in_list' "));
    $in_list = $in_list['value']*15;





    if($seaton == 1){

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,1,1,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,2,1,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,2,2,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,3,1,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,3,2,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,3,3,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,3,4,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,4,1,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,4,2,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,4,3,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,4,4,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,4,5,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,4,6,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,4,7,$time ,$list_num)");

        mysql_query("INSERT INTO erj_seatlist (user_id,seat_money,tree_id ,parent_id,line,seat,time,list_num) VALUES (1,$seatmoney,1,1,4,8,$time ,$list_num)");

        mysql_query("UPDATE erj_sys SET value = 0 WHERE name = 'pond'");

        mysql_query("UPDATE erj_sys SET value = value + $in_list WHERE name = 'list'");

    }



    die("奖池已结算！");

}else{
    die("未到结算时间！");
}



?>